package com.example.demo.service;

import com.example.demo.entity.Manufacturer;
import com.example.demo.entity.Model;
import com.example.demo.entity.Standard;
import com.example.demo.repository.ManufacturerRepository;
import com.example.demo.repository.ModelRepository;
import com.example.demo.repository.StandardRepository;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductService {
    private final ManufacturerRepository manufacturerRepository;
    private final StandardRepository standardRepository;
    private final ModelRepository modelRepository;

    public ProductService(ManufacturerRepository manufacturerRepository,
                          StandardRepository standardRepository,
                          ModelRepository modelRepository) {
        this.manufacturerRepository = manufacturerRepository;
        this.standardRepository = standardRepository;
        this.modelRepository = modelRepository;
    }

    public List<Manufacturer> getAllManufacturers() {
        return manufacturerRepository.findAll();
    }

    public List<Standard> getAllStandards() {
        return standardRepository.findAll();
    }

    public List<Model> getModels(Integer mfrId) {
        if (mfrId == null) {
            // 不筛选时，返回所有型号
            return processModels(modelRepository.findAll());
        } else {
            // 筛选时，返回直接属于该制造商的型号及其所有子型号
            List<Model> parentModels = modelRepository.findByMfrId(mfrId);

            // 收集所有父型号ID
            Set<Integer> parentIds = parentModels.stream()
                    .map(Model::getId)
                    .collect(Collectors.toSet());

            // 递归查找所有子型号
            List<Model> childModels = findAllChildModels(parentIds);

            // 合并并处理型号数据
            List<Model> allModels = new ArrayList<>(parentModels);
            allModels.addAll(childModels);

            return processModels(allModels);
        }
    }

    // 递归查找所有层级的子型号
    private List<Model> findAllChildModels(Set<Integer> parentIds) {
        if (parentIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 将 Set 转换为 List（关键步骤）
        List<Integer> parentIdList = new ArrayList<>(parentIds);

        // 调用修改后的 Repository 方法
        List<Model> directChildren = modelRepository.findBypIdIn(parentIdList);

        // 递归逻辑不变...
        Set<Integer> directChildIds = directChildren.stream()
                .map(Model::getId)
                .collect(Collectors.toSet());

        List<Model> deeperChildren = findAllChildModels(directChildIds);

        List<Model> allChildren = new ArrayList<>(directChildren);
        allChildren.addAll(deeperChildren);

        return allChildren;
    }

    // 处理子型号继承父型号的制造商ID
    private List<Model> processModels(List<Model> models) {
        Map<Integer, Model> modelMap = models.stream()
                .collect(Collectors.toMap(Model::getId, model -> model, (m1, m2) -> m1));

        for (Model model : models) {
            // 若为子型号且自身mfrId为空，则继承父型号的mfrId
            if (model.getpId() != null && model.getMfrId() == null) {
                Model parentModel = modelMap.get(model.getpId());
                if (parentModel != null) {
                    model.setMfrId(parentModel.getMfrId()); // 继承父型号制造商ID
                }
            }
        }

        return models;
    }
}